//输入金额
'use strict';
import React, { Component } from 'react';
import {
  Text,
  TextInput,
  View,
  StyleSheet,
  TouchableOpacity,
  Alert
} from 'react-native';
import NavBar from '../components/NavigatorBar';
import * as defaultStyles from '../constants/DefaultStyles';
import { Actions } from 'react-native-router-flux';

export default class InputMoneyContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      money: '0'
    }
  }

  _checkInput(input: String) {
    if (input.length === 0) {
      Alert.alert('提示', '请输入金额')
      return false;
    }
    let reg = new RegExp(/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/)
    let b = reg.test(input);
    if (!b) {
      Alert.alert('提示', '输入金额格式错误，请输入正数，并保证小数点后至多为两位');
    }
    return b;
  }

  render() {
    return (
      <View style={styles.container}>
        <NavBar leftImage="angle-left" navBarColor={defaultStyles.LIGHTBLUE}
                leftClick={() => {
                  Actions.pop();
                }} title="输入金额"/>
        <View style={styles.textInputView}>
          <TextInput style={{flex: 1}} placeholder={'  金额（元）'}
                     underlineColorAndroid={'transparent'} keyboardType={'numeric'}
                     onChangeText={(text) => this.state.money = text}
          />
        </View>

        <TouchableOpacity style={{marginTop: 20, marginLeft: 20, marginRight: 20, borderRadius: 5, height: 40,
        backgroundColor: defaultStyles.LIGHTBLUE, justifyContent: 'center', alignItems: 'center'}}
                          onPress={() => {
                            let isOK = this._checkInput(this.state.money);
                            if (isOK) {
                              this.props.inputMoney(this.state.money);
                              Actions.pop();
                            }
                          }}>
          <Text style={{color: 'white'}}>确定</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: defaultStyles.WHITEOFBACKGROUND
  },
  textInputView: {
    backgroundColor: 'white',
    marginTop: 20,
    paddingLeft: 15,
    paddingRight: 15,
    height: 40
  },
});